home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / admin / secure / Tor - Privoxy - Vidalia.exe / Tor / Documents / path-spec.txt < prev    next >
Text File  |  2007-08-30  |  19KB  |  409 lines

  1. $Id: path-spec.txt 11258 2007-08-24 06:30:34Z arma $
  2.  
  3.                            Tor Path Specification
  4.  
  5.                               Roger Dingledine
  6.                                Nick Mathewson
  7.  
  8. Note: This is an attempt to specify Tor as currently implemented.  Future
  9. versions of Tor will implement improved algorithms.
  10.  
  11. This document tries to cover how Tor chooses to build circuits and assign
  12. streams to circuits.  Other implementations MAY take other approaches, but
  13. implementors should be aware of the anonymity and load-balancing implications
  14. of their choices.
  15.  
  16.                     THIS SPEC ISN'T DONE OR CORRECT YET.
  17.  
  18. 1. General operation
  19.  
  20.    Tor begins building circuits as soon as it has enough directory
  21.    information to do so (see section 5.1 of dir-spec.txt).  Some circuits are
  22.    built preemptively because we expect to need them later (for user
  23.    traffic), and some are built because of immediate need (for user traffic
  24.    that no current circuit can handle, for testing the network or our
  25.    reachability, and so on).
  26.  
  27.    When a client application creates a new stream (by opening a SOCKS
  28.    connection or launching a resolve request), we attach it to an appropriate
  29.    open circuit if one exists, or wait if an appropriate circuit is
  30.    in-progress. We launch a new circuit only
  31.    if no current circuit can handle the request.  We rotate circuits over
  32.    time to avoid some profiling attacks.
  33.  
  34.    To build a circuit, we choose all the nodes we want to use, and then
  35.    construct the circuit.  Sometimes, when we want a circuit that ends at a
  36.    given hop, and we have an appropriate unused circuit, we "cannibalize" the
  37.    existing circuit and extend it to the new terminus.
  38.  
  39.    These processes are described in more detail below.
  40.  
  41.    This document describes Tor's automatic path selection logic only; path
  42.    selection can be overridden by a controller (with the EXTENDCIRCUIT and
  43.    ATTACHSTREAM commands).  Paths constructed through these means may
  44.    violate some constraints given below.
  45.  
  46. 1.1. Terminology
  47.  
  48.    A "path" is an ordered sequence of nodes, not yet built as a circuit.
  49.  
  50.    A "clean" circuit is one that has not yet been used for any traffic.
  51.  
  52.    A "fast" or "stable" or "valid" node is one that has the 'Fast' or
  53.    'Stable' or 'Valid' flag
  54.    set respectively, based on our current directory information.  A "fast"
  55.    or "stable" circuit is one consisting only of "fast" or "stable" nodes.
  56.  
  57.    In an "exit" circuit, the final node is chosen based on waiting stream
  58.    requests if any, and in any case it avoids nodes with exit policy of
  59.    "reject *:*". An "internal" circuit, on the other hand, is one where
  60.    the final node is chosen just like a middle node (ignoring its exit
  61.    policy).
  62.  
  63.    A "request" is a client-side stream or DNS resolve that needs to be
  64.    served by a circuit.
  65.  
  66.    A "pending" circuit is one that we have started to build, but which has
  67.    not yet completed.
  68.  
  69.    A circuit or path "supports" a request if it is okay to use the
  70.    circuit/path to fulfill the request, according to the rules given below.
  71.    A circuit or path "might support" a request if some aspect of the request
  72.    is unknown (usually its target IP), but we believe the path probably
  73.    supports the request according to the rules given below.
  74.  
  75. 2. Building circuits
  76.  
  77. 2.1. When we build
  78.  
  79. 2.1.1. Clients build circuits preemptively
  80.  
  81.    When running as a client, Tor tries to maintain at least a certain
  82.    number of clean circuits, so that new streams can be handled
  83.    quickly.  To increase the likelihood of success, Tor tries to
  84.    predict what circuits will be useful by choosing from among nodes
  85.    that support the ports we have used in the recent past (by default
  86.    one hour). Specifically, on startup Tor tries to maintain one clean
  87.    fast exit circuit that allows connections to port 80, and at least
  88.    two fast clean stable internal circuits in case we get a resolve
  89.    request or hidden service request (at least three if we _run_ a
  90.    hidden service).
  91.  
  92.    After that, Tor will adapt the circuits that it preemptively builds
  93.    based on the requests it sees from the user: it tries to have two fast
  94.    clean exit circuits available for every port seen within the past hour
  95.    (each circuit can be adequate for many predicted ports -- it doesn't
  96.    need two separate circuits for each port), and it tries to have the
  97.    above internal circuits available if we've seen resolves or hidden
  98.    service activity within the past hour. If there are 12 or more clean
  99.    circuits open, it doesn't open more even if it has more predictions.
  100.  
  101.    Only stable circuits can "cover" a port that is listed in the
  102.    LongLivedPorts config option. Similarly, hidden service requests
  103.    to ports listed in LongLivedPorts make us create stable internal
  104.    circuits.
  105.  
  106.    Note that if there are no requests from the user for an hour, Tor
  107.    will predict no use and build no preemptive circuits.
  108.  
  109.    The Tor client SHOULD NOT store its list of predicted requests to a
  110.    persistent medium.
  111.  
  112. 2.1.2. Clients build circuits on demand
  113.  
  114.    Additionally, when a client request exists that no circuit (built or
  115.    pending) might support, we create a new circuit to support the request.
  116.    For exit connections, we pick an exit node that will handle the
  117.    most pending requests (choosing arbitrarily among ties), launch a
  118.    circuit to end there, and repeat until every unattached request
  119.    might be supported by a pending or built circuit. For internal
  120.    circuits, we pick an arbitrary acceptable path, repeating as needed.
  121.  
  122.    In some cases we can reuse an already established circuit if it's
  123.    clean; see Section 2.3 (cannibalizing circuits) for details.
  124.  
  125. 2.1.3. Servers build circuits for testing reachability and bandwidth
  126.  
  127.    Tor servers test reachability of their ORPort once they have
  128.    successfully built a circuit (on start and whenever their IP address
  129.    changes). They build an ordinary fast internal circuit with themselves
  130.    as the last hop. As soon as any testing circuit succeeds, the Tor
  131.    server decides it's reachable and is willing to publish a descriptor.
  132.  
  133.    We launch multiple testing circuits (one at a time), until we
  134.    have NUM_PARALLEL_TESTING_CIRC (4) such circuits open. Then we
  135.    do a "bandwidth test" by sending a certain number of relay drop
  136.    cells down each circuit: BandwidthRate * 10 / CELL_NETWORK_SIZE
  137.    total cells divided across the four circuits, but never more than
  138.    CIRCWINDOW_START (1000) cells total. This exercises both outgoing and
  139.    incoming bandwidth, and helps to jumpstart the observed bandwidth
  140.    (see dir-spec.txt).
  141.  
  142.    Tor servers also test reachability of their DirPort once they have
  143.    established a circuit, but they use an ordinary exit circuit for
  144.    this purpose.
  145.  
  146. 2.1.4. Hidden-service circuits
  147.  
  148.    See section 4 below.
  149.  
  150. 2.1.5. Rate limiting of failed circuits
  151.  
  152.    If we fail to build a circuit N times in a X second period (see Section
  153.    2.3 for how this works), we stop building circuits until the X seconds
  154.    have elapsed.
  155.    XXXX
  156.  
  157. 2.1.6. When to tear down circuits
  158.  
  159.    XXXX
  160.  
  161. 2.2. Path selection and constraints
  162.  
  163.    We choose the path for each new circuit before we build it.  We choose the
  164.    exit node first, followed by the other nodes in the circuit.  All paths
  165.    we generate obey the following constraints:
  166.      - We do not choose the same router twice for the same path.
  167.      - We do not choose any router in the same family as another in the same
  168.        path.
  169.      - We do not choose more than one router in a given /16 subnet
  170.        (unless EnforceDistinctSubnets is 0).
  171.      - We don't choose any non-running or non-valid router unless we have
  172.        been configured to do so. By default, we are configured to allow
  173.        non-valid routers in "middle" and "rendezvous" positions.
  174.      - If we're using Guard nodes, the first node must be a Guard (see 5
  175.        below)
  176.      - XXXX Choosing the length
  177.  
  178.    For circuits that do not need to be not "fast", when choosing among
  179.    multiple candidates for a path element, we choose randomly.
  180.  
  181.    For "fast" circuits, we pick a given router as an exit with probability
  182.    proportional to its advertised bandwidth [the smaller of the 'rate' and
  183.    'observed' arguments to the "bandwidth" element in its descriptor].  If a
  184.    router's advertised bandwidth is greater than MAX_BELIEVABLE_BANDWIDTH
  185.    (10 MB/s), we clip to that value.
  186.  
  187.    For non-exit positions on "fast" circuits, we pick routers as above, but
  188.    we weight the clipped advertised bandwidth of Exit-flagged nodes depending
  189.    on the fraction of bandwidth available from non-Exit nodes.  Call the
  190.    total clipped advertised bandwidth for Exit nodes under consideration E,
  191.    and the total clipped advertised bandwidth for all nodes under
  192.    consideration T.  If E<T/3, we do not consider Exit-flagged nodes.
  193.    Otherwise, we weight their bandwidth with the factor (E-T/3)/E. This 
  194.    ensures that bandwidth is evenly distributed over nodes in 3-hop paths.
  195.  
  196.    Similarly, guard nodes are weighted by the factor (G-T/3)/G, and not
  197.    considered for non-guard positions if this value is less than 0.
  198.  
  199.    Additionally, we may be building circuits with one or more requests in
  200.    mind.  Each kind of request puts certain constraints on paths:
  201.  
  202.      - All service-side introduction circuits and all rendezvous paths
  203.        should be Stable.
  204.      - All connection requests for connections that we think will need to
  205.        stay open a long time require Stable circuits.  Currently, Tor decides
  206.        this by examining the request's target port, and comparing it to a
  207.        list of "long-lived" ports. (Default: 21, 22, 706, 1863, 5050,
  208.        5190, 5222, 5223, 6667, 6697, 8300.)
  209.      - DNS resolves require an exit node whose exit policy is not equivalent
  210.        to "reject *:*".
  211.      - Reverse DNS resolves require a version of Tor with advertised eventdns
  212.        support (available in Tor 0.1.2.1-alpha-dev and later).
  213.      - All connection requests require an exit node whose exit policy
  214.        supports their target address and port (if known), or which "might
  215.        support it" (if the address isn't known).  See 2.2.1.
  216.      - Rules for Fast? XXXXX
  217.  
  218. 2.2.1. Choosing an exit
  219.  
  220.    If we know what IP address we want to connect to or resolve, we can
  221.    trivially tell whether a given router will support it by simulating
  222.    its declared exit policy.
  223.  
  224.    Because we often connect to addresses of the form hostname:port, we do not
  225.    always know the target IP address when we select an exit node.  In these
  226.    cases, we need to pick an exit node that "might support" connections to a
  227.    given address port with an unknown address.  An exit node "might support"
  228.    such a connection if any clause that accepts any connections to that port
  229.    precedes all clauses (if any) that reject all connections to that port.
  230.  
  231.    Unless requested to do so by the user, we never choose an exit server
  232.    flagged as "BadExit" by more than half of the authorities who advertise
  233.    themselves as listing bad exits.
  234.  
  235. 2.2.2. User configuration
  236.  
  237.    Users can alter the default behavior for path selection with configuration
  238.    options.
  239.  
  240.    - If "ExitNodes" is provided, then every request requires an exit node on
  241.      the ExitNodes list.  (If a request is supported by no nodes on that list,
  242.      and StrictExitNodes is false, then Tor treats that request as if
  243.      ExitNodes were not provided.)
  244.  
  245.    - "EntryNodes" and "StrictEntryNodes" behave analogously.
  246.  
  247.    - If a user tries to connect to or resolve a hostname of the form
  248.      <target>.<servername>.exit, the request is rewritten to a request for
  249.      <target>, and the request is only supported by the exit whose nickname
  250.      or fingerprint is <servername>.
  251.  
  252. 2.3. Cannibalizing circuits
  253.  
  254.    If we need a circuit and have a clean one already established, in
  255.    some cases we can adapt the clean circuit for our new
  256.    purpose. Specifically,
  257.  
  258.    For hidden service interactions, we can "cannibalize" a clean internal
  259.    circuit if one is available, so we don't need to build those circuits
  260.    from scratch on demand.
  261.  
  262.    We can also cannibalize clean circuits when the client asks to exit
  263.    at a given node -- either via the ".exit" notation or because the
  264.    destination is running at the same location as an exit node.
  265.  
  266.  
  267. 2.4. Handling failure
  268.  
  269.    If an attempt to extend a circuit fails (either because the first create
  270.    failed or a subsequent extend failed) then the circuit is torn down and is
  271.    no longer pending.  (XXXX really?)  Requests that might have been
  272.    supported by the pending circuit thus become unsupported, and a new
  273.    circuit needs to be constructed.
  274.  
  275.    If a stream "begin" attempt fails with an EXITPOLICY error, we
  276.    decide that the exit node's exit policy is not correctly advertised,
  277.    so we treat the exit node as if it were a non-exit until we retrieve
  278.    a fresh descriptor for it.
  279.  
  280.    XXXX
  281.  
  282. 3. Attaching streams to circuits
  283.  
  284.    When a circuit that might support a request is built, Tor tries to attach
  285.    the request's stream to the circuit and sends a BEGIN, BEGIN_DIR,
  286.    or RESOLVE relay
  287.    cell as appropriate.  If the request completes unsuccessfully, Tor
  288.    considers the reason given in the CLOSE relay cell. [XXX yes, and?]
  289.  
  290.  
  291.    After a request has remained unattached for SocksTimeout (2 minutes
  292.    by default), Tor abandons the attempt and signals an error to the
  293.    client as appropriate (e.g., by closing the SOCKS connection).
  294.  
  295.    XXX Timeouts and when Tor auto-retries.
  296.     * What stream-end-reasons are appropriate for retrying.
  297.  
  298.    If no reply to BEGIN/RESOLVE, then the stream will timeout and fail.
  299.  
  300. 4. Hidden-service related circuits
  301.  
  302.   XXX Tracking expected hidden service use (client-side and hidserv-side)
  303.  
  304. 5. Guard nodes
  305.  
  306.   We use Guard nodes (also called "helper nodes" in the literature) to
  307.   prevent certain profiling attacks.  Here's the risk: if we choose entry and
  308.   exit nodes at random, and an attacker controls C out of N servers
  309.   (ignoring advertised bandwidth), then the
  310.   attacker will control the entry and exit node of any given circuit with
  311.   probability (C/N)^2.  But as we make many different circuits over time,
  312.   then the probability that the attacker will see a sample of about (C/N)^2
  313.   of our traffic goes to 1.  Since statistical sampling works, the attacker
  314.   can be sure of learning a profile of our behavior.
  315.  
  316.   If, on the other hand, we picked an entry node and held it fixed, we would
  317.   have probability C/N of choosing a bad entry and being profiled, and
  318.   probability (N-C)/N of choosing a good entry and not being profiled.
  319.  
  320.   When guard nodes are enabled, Tor maintains an ordered list of entry nodes
  321.   as our chosen guards, and stores this list persistently to disk.  If a Guard
  322.   node becomes unusable, rather than replacing it, Tor adds new guards to the
  323.   end of the list.  When choosing the first hop of a circuit, Tor
  324.   chooses at
  325.   random from among the first NumEntryGuards (default 3) usable guards on the
  326.   list.  If there are not at least 2 usable guards on the list, Tor adds
  327.   routers until there are, or until there are no more usable routers to add.
  328.  
  329.   A guard is unusable if any of the following hold:
  330.     - it is not marked as a Guard by the networkstatuses,
  331.     - it is not marked Valid (and the user hasn't set AllowInvalid entry)
  332.     - it is not marked Running
  333.     - Tor couldn't reach it the last time it tried to connect
  334.  
  335.   A guard is unusable for a particular circuit if any of the rules for path
  336.   selection in 2.2 are not met.  In particular, if the circuit is "fast"
  337.   and the guard is not Fast, or if the circuit is "stable" and the guard is
  338.   not Stable, or if the guard has already been chosen as the exit node in
  339.   that circuit, Tor can't use it as a guard node for that circuit.
  340.  
  341.   If the guard is excluded because of its status in the networkstatuses for
  342.   over 30 days, Tor removes it from the list entirely, preserving order.
  343.  
  344.   If Tor fails to connect to an otherwise usable guard, it retries
  345.   periodically: every hour for six hours, every 4 hours for 3 days, every
  346.   18 hours for a week, and every 36 hours thereafter.  Additionally, Tor
  347.   retries unreachable guards the first time it adds a new guard to the list,
  348.   since it is possible that the old guards were only marked as unreachable
  349.   because the network was unreachable or down.
  350.  
  351.   Tor does not add a guard persistently to the list until the first time we
  352.   have connected to it successfully.
  353.  
  354.  
  355.  
  356.  
  357.  
  358. X. Old notes
  359.  
  360. X.1. Do we actually do this?
  361.  
  362. How to deal with network down.
  363.   - While all helpers are down/unreachable and there are no established
  364.     or on-the-way testing circuits, launch a testing circuit. (Do this
  365.     periodically in the same way we try to establish normal circuits
  366.     when things are working normally.)
  367.     (Testing circuits are a special type of circuit, that streams won't
  368.     attach to by accident.)
  369.   - When a testing circuit succeeds, mark all helpers up and hold
  370.     the testing circuit open.
  371.   - If a connection to a helper succeeds, close all testing circuits.
  372.     Else mark that helper down and try another.
  373.   - If the last helper is marked down and we already have a testing
  374.     circuit established, then add the first hop of that testing circuit
  375.     to the end of our helper node list, close that testing circuit,
  376.     and go back to square one. (Actually, rather than closing the
  377.     testing circuit, can we get away with converting it to a normal
  378.     circuit and beginning to use it immediately?)
  379.  
  380.   [Do we actually do any of the above?  If so, let's spec it.  If not, let's
  381.   remove it. -NM]
  382.  
  383. X.2. A thing we could do to deal with reachability.
  384.  
  385. And as a bonus, it leads to an answer to Nick's attack ("If I pick
  386. my helper nodes all on 18.0.0.0:*, then I move, you'll know where I
  387. bootstrapped") -- the answer is to pick your original three helper nodes
  388. without regard for reachability. Then the above algorithm will add some
  389. more that are reachable for you, and if you move somewhere, it's more
  390. likely (though not certain) that some of the originals will become useful.
  391. Is that smart or just complex?
  392.  
  393. X.3. Some stuff that worries me about entry guards. 2006 Jun, Nickm.
  394.  
  395.   It is unlikely for two users to have the same set of entry guards.
  396.   Observing a user is sufficient to learn its entry guards.  So, as we move
  397.   around, entry guards make us linkable.  If we want to change guards when
  398.   our location (IP? subnet?) changes, we have two bad options.  We could
  399.     - Drop the old guards.  But if we go back to our old location,
  400.       we'll not use our old guards.  For a laptop that sometimes gets used
  401.       from work and sometimes from home, this is pretty fatal.
  402.     - Remember the old guards as associated with the old location, and use
  403.       them again if we ever go back to the old location.  This would be
  404.       nasty, since it would force us to record where we've been.
  405.  
  406.   [Do we do any of this now? If not, this should move into 099-misc or
  407.   098-todo. -NM]
  408.  
  409.